package ManagerSystem;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.MapListHandler;

import tjut.db.DBDriverFactory;


/**
 * 登录
 * @author ChrisL
 *
 */
public class Login {

	
	public void UserLog(){
		System.out.println("****************用户登录******************");
		System.out.println("*               1.学生登录                                             *");
		System.out.println("*               2.老师登录                                             *");
		System.out.println("*               3.退出                                                     *");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入你的选择：");
		int a=input.nextInt();
		
		if(a==1){
			
			System.out.println("请输入学生ID：");
			String UserStu=input.next();
			//先设置布尔变量
			boolean success=true;
			//判断ID是否存在
			if(IsID(UserStu)){
				//进行do while循环 如果密码错误就重新输入
				do{
					System.out.println("请输入学生密码：");
					String UserPsw=input.next();
					//传参 登陆成功给false退出循环
					if(IsPSW(UserStu,UserPsw)){
						System.out.println("登陆成功");
						success=false;
					}else{
						System.out.println("密码错误,请重新输入");
					}
				}while(success);
				//调用学生功能菜单
				Student stu=new Student();
				stu.Function(UserStu);
			}else{
				System.out.println("用户不存在，新建中");
				AddStuInfo asi=new AddStuInfo();
				asi.Info();
				this.UserLog();
			}
			
			
		}
		else if(a==2){
			System.out.println("请输入老师ID：");
			String UserTea=input.next();
			
			boolean success=true;
			
			if(IsTID(UserTea)){
				do{
					System.out.println("请输入老师密码：");
					String UserTPsw=input.next();
					if(IsTPSW(UserTea,UserTPsw)){
						System.out.println("登陆成功");
						success=false;
					}else{
						System.out.println("密码错误,请重新输入");
					}
				}while(success);
				Teacher t=new Teacher();
				t.Function();
			}else{
				System.out.println("用户不存在，请联系系统管理员");
				
			}
		}
		else{
			System.out.println("已退出");
		}
	}
	
	public boolean IsID(String UserStu){

		String strSQL = "SELECT * FROM MANAGERSYS WHERE STU_ID = ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSQL, new MapListHandler(),new Object[]{UserStu});
		return lst != null && lst.size() > 0 ? true : false; 

				
	}

	public boolean IsPSW(String UserStu,String UserPsw){
		
		String strSQL = "SELECT * FROM MANAGERSYS WHERE STU_ID='"+UserStu +"' AND STU_PSW = ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSQL, new MapListHandler(),new Object[]{UserPsw});
		return lst != null && lst.size() > 0? true : false; 

				
	}
	
	public boolean IsTID(String UserTea){

		String strSQL = "SELECT * FROM PASSWORD WHERE TEA_NAME = ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSQL, new MapListHandler(),new Object[]{UserTea});
		return lst != null && lst.size() > 0 ? true : false; 

				
	}

	public boolean IsTPSW(String UserTea,String UserTPsw){
		
		String strSQL = "SELECT * FROM PASSWORD WHERE TEA_NAME='"+UserTea +"' AND PSW = ?";
		List lst =DBDriverFactory.getDBDriverInstance().query(strSQL, new MapListHandler(),new Object[]{UserTPsw});
		return lst != null && lst.size() > 0? true : false; 

				
	}
	
}
